package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UpdateAccountServlet extends HttpServlet {
    private static final String ACCOUNT_FORM="/WEB-INF/jsp/account/IncludeAccountFields.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        Account account = (Account) session.getAttribute("loginAccount");

        session.setAttribute("firstName",req.getParameter("account.firstName"));
        session.setAttribute("lastName",req.getParameter("account.lastName"));
        session.setAttribute("email",req.getParameter("account.email"));
        session.setAttribute("phone",req.getParameter("account.phone"));
        session.setAttribute("address1",req.getParameter("account.address1"));
        session.setAttribute("address2",req.getParameter("account.address2"));
        session.setAttribute("city",req.getParameter("account.city"));
        session.setAttribute("state",req.getParameter("account.state"));
        session.setAttribute("country",req.getParameter("account.country"));
        session.setAttribute("zip",req.getParameter("account.zip"));

        AccountDao accountDao=new AccountDaoImpl();

        account.setFirstName(session.getAttribute("firstName").toString());
        account.setLastName(session.getAttribute("lastName").toString());
        account.setEmail(session.getAttribute("email").toString());
        account.setPhone(session.getAttribute("phone").toString());
        account.setAddress1(session.getAttribute("address1").toString());
        account.setAddress2(session.getAttribute("address2").toString());
        account.setCity(session.getAttribute("city").toString());
        account.setState(session.getAttribute("state").toString());
        account.setCountry(session.getAttribute("country").toString());
        account.setZip(session.getAttribute("zip").toString());
        account.setStatus("OK");

        accountDao.updateAccount(account);
        req.getRequestDispatcher(ACCOUNT_FORM).forward(req, resp);
    }
}
